<template>
  <div id="root">
    <div class="todo-container">
      <div class="todo-wrap">
        <TodoHeader :Todos="Todos" @addTodo="addTodo"></TodoHeader>
        <TodoMain :Todos="Todos" :abab="abab"></TodoMain>
        <TodoFooter
          @DeleteAll="DeleteAll"
          @CheckedAll="CheckedAll"
          :Todos="Todos"
        ></TodoFooter>
      </div>
    </div>
  </div>
</template>

<script>
import TodoHeader from "@/components/Todo/Header";
import TodoMain from "@/components/Todo/Main";
import TodoFooter from "@/components/Todo/Footer";
export default {
  name: "TodoList",
  data() {
    return {
      // Todos: [
      //   { id: 1001, content: "吃饭", done: false },
      //   { id: 1002, content: "睡觉", done: true },
      //   { id: 1003, content: "打代码", done: false },
      // ],
      Todos: JSON.parse(localStorage.getItem("Todos")) || [],
    };
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  watch: {
    Todos: {
      handler() {
        localStorage.setItem("Todos", JSON.stringify(this.Todos));
      },
      deep: true,
    },
  },
  mounted() {
    if (this.Todos.length !== 0) {
      this.isShow = false;
    }
  },
  created() { 
    this.$on.a=1
    this.$bus.$on("chenckTodo", this.chenckTodo);
    this.$bus.$on("DeleteTodo", this.DeleteTodo);
  },
  methods: {
    addTodo(val) {
      this.Todos.push({ id: Date.now(), content: val, done: false });
    },
    chenckTodo(index) {
      this.Todos[index].done = !this.Todos[index].done;
    },
    DeleteTodo(index) {
      this.Todos.splice(index, 1);
    },
    CheckedAll(done) {
      this.Todos.forEach((item) => (item.done = done));
    },
    DeleteAll() {
      this.Todos = this.Todos.filter((item) => !item.done);
    },
    abab(_this) {
      console.log(_this.$on === this.$bus.$on);
      console.log(_this.$emit === this.$bus.$emit);
    },
  },
};
</script>
<style scoped>
/*app*/
.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>